package com.eportal.action.background;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.action.background.pojo.Page;
import com.eportal.domain.LifeResource;
import com.eportal.domain.Sysopt;
import com.eportal.service.LifeResourceService;
import com.eportal.service.SysmenuService;
import com.eportal.util.ValidateUtil;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;

/**
 *  生活服务管理管理
 */
public class LifeResourceAction extends BaseAction implements ModelDriven<LifeResource> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(LifeResourceAction.class);
	private Map<String, Object> result = new HashMap<String, Object>();
	@Autowired
	private LifeResourceService lifeResourceService;
	@Autowired
	private SysmenuService resourceService;
	private static String LISTVIEWURL="/admin/lifeResource_listView.action";
	
	/**
	 * 列表显示
	 *
	 * @return
	 */

	public String listView() {
		Map<String,String> params = new HashMap<String,String>();
		String flag = getRequest().getParameter("flag");
		if (StringUtils.isBlank(flag)) {
			flag = "1";
		}
		StringBuffer where = new StringBuffer(" and isOnLine");
		Sysopt  user= (Sysopt)getSession().getAttribute("user");
		int userRole =  (Integer) getSession().getAttribute("userRole");
		if (userRole == 5 || userRole == 6) {
				return "error";
			}
		if("1".equals(flag)){
			where.append(" !=");
			where.append(0);
			if (userRole == 1 || userRole == 2) {
				where.append(" and (t.ifShare='1' or t.dType='1') ");
			}
			if (userRole == 21) {
				where.append(" and (t.ifShare='1' or a.sysoptId=");
				where.append(user.getSid());
				where.append(")");
			}
			if (userRole == 3 || userRole == 4) {
				where.append(" and (t.ifShare='1' or t.agtCode='");
				where.append(user.getAgtCode());
				where.append("')");
			}
		}else{
			where.append(" =");
			where.append(0);
			if (userRole == 3 || userRole == 4) {
	    		where.append(" and t.agtCode='");
	    		where.append(user.getAgtCode());
	    		where.append("'");
			}
			if (userRole == 21) {
				where.append(" and a.sysoptId=");
				where.append(user.getSid());
			}
		}
    	
		Page page = new Page();
		String pageNo = getRequest().getParameter("page");
		if (pageNo != null && pageNo.trim().length() > 0) {
			page.setCurrentPage(Integer.parseInt(pageNo));
		}
		
		page = lifeResourceService.find(where.toString(), page);
		List<Map<String, Object>> buttonMap = resourceService.getButton(LISTVIEWURL);
		getRequest().setAttribute("buttonMap", buttonMap);
		JSONArray json = JSONArray.fromObject(buttonMap);
		getRequest().setAttribute("buttonJson", json.toString());
		getRequest().setAttribute("page", page);
		getRequest().setAttribute("flag", flag);
		getRequest().setAttribute("userRole", userRole);
		getRequest().setAttribute("agt", user.getAgtCode());
		return SUCCESS;
	}

	
	/**
	 * 资源添加页面
	 *
	 * @return
	 */
	public String addView() {
		return SUCCESS;
	}
	/**
	 * 图片裁剪页面
	 *
	 * @return
	 */
	public String picView() {
		String path = getRequest().getParameter("path");
		getRequest().setAttribute("path", path);
		return SUCCESS;
	}
	
	/**
	 * 图片裁剪
	 * @throws IOException
	 */
	public void picCut()  throws IOException {
		String path = getRequest().getParameter("path");
		File file = getUpload();
		if(file == null){
			result.put("code", 0);
			result.put("msg", "上传失败，找不到文件");
			responseBody(result);
			return;
		}
		boolean isImage = ValidateUtil.isImage(file);
        if (!isImage) {
       	 	result.put("code", 0);
       	 	result.put("msg", "图片格式不正确");
       	 	responseBody(result);
       	 	return;
        }
    	String fileName = getUploadFileName();
    	String putData = getRequest().getParameter("putData");
    	if(StringUtils.isBlank(putData)){
    		result.put("code", 0);
       	 	result.put("msg", "无法判断剪切范围");
       	 	responseBody(result);
       	 	return;
    	}
    	result = lifeResourceService.picCutUpload(file,fileName,putData,path);
		responseBody(result);
	}
	/**
	 * 资源添加
	 *
	 * @return
	 */
	public void add() throws IOException {
		String msg = ValidateUtil.validate(lifeResource);
		if (msg.length() == 0) {
			LifeResource lifeResource = getModel();
	    	result = lifeResourceService.addMethod(lifeResource);
		} else {
			result.put("code", 0);
			result.put("msg", msg);
		}
		responseBody(result);
	}

	/**
	 * 修改页面
	 *
	 * @return
	 */

	public String editView() {
		String sid = getRequest().getParameter("sid");
		if (StringUtils.isBlank(sid)) {
			return "error";
		}
		Map<String, String>  lifeResource = lifeResourceService.getBySql(Long.parseLong(sid));
		getRequest().setAttribute("lifeResource", lifeResource);
		return SUCCESS;
	}

	/**
	 * 修改
	 *
	 * @return
	 */
	public void edit() throws IOException {
		String msg = ValidateUtil.validate(lifeResource);
		if (msg.length() == 0) {
			LifeResource lifeResource = getModel();
	    	result = lifeResourceService.updateMethod(lifeResource);
		} else {
			result.put("code", -1);
			result.put("msg", msg);
		}
		responseBody(result);
	}

	
	@Override
	public LifeResource getModel() {
		return lifeResource;
	}

	private LifeResource lifeResource = new LifeResource();
}
